// 点击返回上一级
document.querySelector('.back').addEventListener('click', () => history.back())

axios.defaults.baseURL = 'http://cba.itlike.com/public/index.php?s=/api/'

// 检测到本地存储后渲染对应的列表
async function render() {
    const res = await axios.get('/goods/list', {
        params: {
            goodsName: localStorage.getItem('goodsList')
        }
    })
    console.log(res.data.data.list.data)
    const goodlists = res.data.data.list.data
    
    document.querySelector('.list_content').innerHTML = goodlists.map(ele => `
        <li>
          <a href="#">
            <div class="pic">
              <img data-id="${ele.goods_id}" src="${ele.goods_image}" alt="">
            </div>
            <div class="info">
              <div data-id="${ele.goods_id}" class="txt ellipsis-2">${ele.goods_name}</div>
              <div data-id="${ele.goods_id}" class="num"><span>已售${ele.goods_sales}件</span></div>
              <div class="price">
                <span data-id="${ele.goods_id}" class="one">￥<i>${ele.goods_price_max}</i></span>
                <span data-id="${ele.goods_id}" class="two">${ele.line_price_max==='0.00'?'':`￥<i>${ele.line_price_max}</i>`}</span>
              </div>
            </div>
          </a>
        </li>
    `).join('')
    // 点击商品列表 跳转商品详情
    document.querySelector('.list_content').addEventListener('click',function (e) {
        localStorage.setItem('goodsInformation', e.target.dataset.id)
        location.href = './commodity.html'
    })
}
render()

// 点击排列按钮更改样式
// 开关
let flage = 1
document.querySelector('.more').addEventListener('click', function () {
    if (flage) {
        document.querySelector('.list_content').classList.remove('list_content')
        document.querySelector('#list_content').classList.add('change')
        flage = 0
    } else {
        document.querySelector('.change').classList.remove('change')
        document.querySelector('#list_content').classList.add('list_content')
        flage = 1
    }
})